<?php

/**
 * alltosun.com 余额记录控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2014-11-17 下午6:41:46 $
 * $Id: index.php 156254 2015-12-17 03:28:07Z gaoa $
 */
class Action
{
    private $per_page = 30;

    /**
     * 为用户添加余额
     */
    public function add_user_money() {
        $id = Request::getParam('id', 0);

        if (!$id) {
            return false;
        }

        $user_info = _uri('user', $id);
        if (!$user_info) {
            return '此用户不存在!';
        }

        $filter = array('parent_id' => 0, 'status' => 1);
        $parent_type_list = _model('user_money_type')->getList($filter);

        $parent_ids = array();
        $user_money_type_list = array();
        if ($parent_type_list) {
            foreach ($parent_type_list as $key => $value) {

                array_push($parent_ids, $value['id']);
                $user_money_type_list[$key]['id'] = $value['id'];

                $user_money_type_list[$key]['res_name'] = $value['res_name'];
                $user_money_type_list[$key]['type_list'] = _model('user_money_type')->getList(array(
                    'parent_id' => $value['id'],
                    'status' => 1
                ));
            }
        }

        Response::assign('user_info', $user_info);
        Response::assign('user_money_type_list', $user_money_type_list);
        Response::display('admin/add_user_money.html');
    }

    /**
     * 后台人工修改余额
     */
    public function change() {
        $user_money_info = Request::getParam('user_money', array());

        if (isset($user_money_info['user_money'])) {
            if (!is_numeric($user_money_info['user_money'])) {
                return '用户余额只能填数字';
            }
        } else {
            return '用户余额不能为空';
        }

        $data = array();
        $data['user_id'] = $user_money_info['user_id'];
        $data['admin_id'] = user_helper::get_user_id();
        $data['user_money'] = $user_money_info['user_money'];
        $data['res_id'] = $user_money_info['res_id'];
        $data['extra'] = $user_money_info['extra'];

        $user_info = _uri('user', $data['user_id']);
        if ($user_info) {
            $ret = user_money_helper::change_user_money($data);
            if (!$ret) {
                return '添加失败!';
            }
        } else {
            return '未找到此用户信息!';
        }

        Response::assign('status', 1);
        Response::redirect('user_money/admin/user_money');

    }


    /**
     * 余额记录列表
     */
    public function index() {
        $page_no = $status = $search_filter = $res_id = $is_excel = null;

        extract(URLTracker::init(array(
            'page_no' => 1,
            'status' => 1,
            'search_filter' => array(),
            'res_id' => '0',
            'is_excel' => '0',
        )));

        //过滤条件
        $filter = array();

        // 用户名的过滤
        if (isset($search_filter['user_name']) && $search_filter['user_name']) {
            $ids = user_helper::get_user_ids_by_name($search_filter['user_name']);
            $filter['user_id'] = $ids;
        }
        // 操作人的过滤
        if (isset($search_filter['admin_name']) && $search_filter['admin_name']) {
            $ids = user_helper::get_user_ids_by_name($search_filter['admin_name']);
            $filter['admin_id'] = $ids;
        }

        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'] . ' 00:00:00';
        }

        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'] . ' 23:59:59';
        }

        if ($res_id != '0') {
            $filter['res_id'] = $res_id;
        }

        if (!$filter) {
            $filter['user_id >'] = 0;
        }

        $table_name = 'user_money_record';

        if ($is_excel == 1) { // 导出
            if (empty($search_filter['start_date']) && empty($search_filter['end_date'])) {
                return '请先选择需要导出的时间范围,点击搜索按钮查询结果,再导出excel';
            } elseif (empty($search_filter['start_date'])) {
                $filter['add_time >='] = date('Y-m-d', strtotime($search_filter['end_date']) - (364 * 86400)) .
                                         ' 00:00:00';
            } elseif (empty($search_filter['end_date'])) {
                $filter['add_time <='] = date('Y-m-d') . ' 23:59:59';
            }
            if (abs(strtotime($filter['add_time >=']) - strtotime($filter['add_time <='])) > 365 * 86400) {
                return '时间跨度过大, 请选择范围一年以内';
            }
            $user_money_list = get_data_list_all($table_name, $filter);
        } else {
            $user_money_list = get_data_list($table_name, $filter, ' ORDER BY `id` DESC ', $page_no, $this->per_page);
        }

        if ($user_money_list) {

            // $user_money_list = user_money_helper::user_money_record_format($user_money_list);
            // 分配用户名和操作人姓名
            foreach ($user_money_list as $k => $v) {
                // 余额类型
                $type_info = _uri('user_money_type', $v['res_id']);
                if ($type_info && $type_info['status'] == 1) {
                    $user_money_list[$k]['res_name'] = $type_info['res_name'];
                } else {
                    $user_money_list[$k]['res_name'] = '其他途径';
                }
                $user_info = _uri('user', $v['user_id']);
                $user_money_list[$k]['user_name'] = $user_info['user_name'];
                $user_money_list[$k]['realname'] = $user_info['realname'];

                if ($v['admin_id']) {
                    $admin_name = _uri('user', $v['admin_id'], 'realname');
                    $user_money_list[$k]['admin_name'] = $admin_name;
                } else {
                    $user_money_list[$k]['admin_name'] = '系统';
                }

                if ($v['order_id']) {
                    $order_info = order_helper::get_order_detail($v['order_id']);
                    $store_info = store_helper::get_store($order_info['store_id']);
                    $agency_info = _uri('user', $order_info['agency_id']);
                    $user_money_list[$k]['order_info'] = $order_info;
                    $user_money_list[$k]['store_info'] = $store_info;
                    $user_money_list[$k]['agency_info'] = $agency_info;
                }
            }
        }

        // 导出二次处理
        if ($is_excel == 1) {
            $file = $table_name .
                    (!empty($search_filter['user_name']) ? '_' . $search_filter['user_name'] : '') .
                    (!empty($search_filter['admin_name']) ? '_' . $search_filter['admin_name'] : '') .
                    (!empty($search_filter['start_date']) ? '_' . $search_filter['start_date'] : '_~') .
                    (!empty($search_filter['end_date']) ? '_' . $search_filter['end_date'] : '_~');
            $this->export_excel($file, $user_money_list);
        }

        $user_money_type = _model('user_money_type')->getList(array(1 => 1));

        Response::assign('user_money_list', $user_money_list);
        Response::assign('user_money_type', $user_money_type);
        Response::assign('status', $status);
        // Response::assign('search_filter', $search_filter);

        Response::display('admin/user_money_record_list.html');
    }

    /**
     * 伪删除
     */
    public function delete() {
        $id = Request::getParam('id');

        $ids = explode(',', $id);
        foreach ($ids as $k => $v) {
            _model('user_money_record')->update($v, array('status' => 0));
        }

        return 'ok';
    }

    /**
     * 恢复
     */
    public function change_status() {
        //接收id
        $id = Request::Get('id', 1);

        _model('user_money_record')->update($id, array('status' => 1));

        Response::assign('status', 1);
        Response::redirect(AnUrl('user_money/admin?status=1'));
    }

    /**
     * 物理删除
     */
    public function delete_record() {
        //接收id
        $id = Request::getParam('id');

        $ids = explode(',', $id);
        foreach ($ids as $k => $v) {
            _model('user_money_record')->delete($v);
        }

        return 'ok';
    }

    /**
     * 接受ajax传递的用户名,进行查询
     */
    public function ajax_search() {
        $user_name = Request::getParam('user_name', '');

        if (!$user_name) {
            echo 0;
        }
        //根据接受的值查询符合条件的用户
        $user_ids = user_helper::get_user_ids_by_name($user_name);

        // $user_id_list = explode(',', $user_ids);

        $user_list = array();
        foreach ($user_ids as $k => $v) {
            array_push($user_list, user_helper::get_user_info($v));
        }
        if ($user_list) {
            echo json_encode($user_list);
        } else {
            echo 0;
        }

    }

    /**
     * 接受ajax传递的用户id,获取对应旗下的分店列表
     */
    public function ajax_search_store_list() {
        $user_id = Request::getParam('user_id', 1);

        if (!$user_id) {
            echo 0;
        }

        $store_list = array();

        // 根据用户id获取旗下的商铺列表
        $store_list = store_helper::get_store($user_id);

        if ($store_list) {
            echo json_encode($store_list);
        } else {
            echo 0;
        }

    }

    private function export_excel($file, $data){
        if(!$data){
            return;
        }

        $export[] = explode(',', '用户名, 姓名, 余额, 来源, 操作人, 记录时间, 备注, 店铺id, 店铺名称, 合伙人id, 合伙人姓名, 订单号');

        $send = 0;
        $back = 0;
        foreach ($data as $k => $v) {
            if ($v['user_money'] > 0) {
                $send += $v['user_money'];
            } elseif ($v['user_money'] < 0) {
                $back += $v['user_money'];
            }

            $store_id = $store_title = $agency_id = $agency_realname = $order_no = '';
            if (isset($v['store_info']) && $v['store_info']) {
                $store_id = $v['store_info']['id'];
                $store_title = $v['store_info']['title'] . '(' . $v['store_info']['title_branch'] . ')';
            }

            if (isset($v['agency_info']) && $v['agency_info']) {
                $agency_id = $v['agency_info']['id'];
                $agency_realname = $v['agency_info']['realname'];
            }

            if (isset($v['order_info']) && $v['order_info']) {
                $order_no = $v['order_info']['order_no'];
            }

            $export[] = array(
                $v['user_name'],
                $v['realname'],
                num_keep_two_round($v['user_money']),
                $v['res_name'],
                $v['admin_name'],
                $v['add_time'],
                $v['extra'],
                $store_id,
                $store_title,
                $agency_id,
                $agency_realname,
                $order_no,
            );
        }

        array_unshift($export, array(
            '发放总计',
            num_keep_two_round($send),
            '回收总计',
            num_keep_two_round(-1 * $back),
        ), array());

        _widget('tools.excel')->export($file, array(
            array(
                'name' => 'sheet',
                'data' => array(
                    'data' => $export,
                ),
            )
        ));
    }
}

?>